const path = require("path");
const fs = require("fs");
const pkgJson = require('../package.json');
const getEnv = require("./envs");

// 获取当前打包参数
const BUILD_TYPE = process.env.BUILD_TYPE;
const env = getEnv(BUILD_TYPE);
console.log('本次传入的环境参数: ', JSON.stringify(env, null, 2));

const resolve = (src) => path.resolve(__dirname, src);

process.env.NODE_ENV = 'production';

// 引用别名
const aliasEntry = resolve('../src');
const alias = fs
  .readdirSync(aliasEntry)
  .filter((pathname) => fs.statSync(path.join(aliasEntry, pathname)).isDirectory())
  .reduce((prev, curr) => {
    return Object.assign(prev, { [`@/${curr}`]: resolve(`${aliasEntry}/${curr}`) });
  }, {});


const config = {
  projectName: pkgJson.names,
  date: '2021-1-31',
  designWidth: 750,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2
  },
  sourceRoot: 'src',
  outputRoot: 'dist',
  plugins: [],
  defineConstants: {
  },
  copy: {
    patterns: [
    ],
    options: {
    }
  },
  alias,
  env,
  framework: 'react',
  mini: {
    postcss: {
      pxtransform: {
        enable: true,
        config: {

        }
      },
      url: {
        enable: true,
        config: {
          limit: 1024 // 设定转换尺寸上限
        }
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    }
  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      autoprefixer: {
        enable: true,
        config: {
        }
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    }
  }
}

module.exports = (merge) => merge({}, config);
